---
title: 'Replication Codes for ''***The (In)effectiveness of Populist Rhetoric: A Conjoint Experiment of Campaign Messaging***'''
author: "Yaoyao Dai and Alexander Kustov"
output:
  html_notebook:
    df_print: paged
    theme: cerulean
    toc: yes
    toc_depth: 3
    toc_float:
      collapsed: yes
      smooth_scroll: yes
  html_document:
    df_print: paged
    theme: cerulean
    highlight: zenburn
    toc: yes
    toc_depth: 3
    toc_float:
      collapsed: yes
      smooth_scroll: yes
editor_options:
  chunk_output_type: inline
---

***
This is an R notebook with codes to replicate all the tables and figures in the paper *"The (In)effectiveness of Populist Rhetoric: A Conjoint Experiment of Campaign Messaging"* and its Online Appendices. To replicate the analysis, please download the replication data to the same folder as this .rmd file and run the .rmd file in R. You can also copy and paste the code chunks to a .r file and run the codes only. 

# Load data and packages
```{r results='hide', message=FALSE}
rm(list=ls(all=TRUE))
options(scipen=999)
library(plyr)
library(tidyverse)

library(lmtest)
library(multiwayvcov)
library(sandwich)
library(stargazer)
library(TOSTER)

library(ggplot2)
library(cregg)

library(estimatr)
```

```{r}
load("Conjoint_clean.RData")
```

# Codebook

Variable Name | Variable Type | Values  
--|----|:----
job2 | candidate attribute - job background|elite; non-elite
office2 | candidate attribute - political experience  |held office; didn't hold office
polls2 | candidate attribute - current polling  |leading; trailing
immigration2congruent | candidate attribute - immigration position  |non-congruent; congruent
economy2congruent | candidate attribute - economic position  | non-congruent; congruent
antipluralism3 | candidate attribute - pluralism \& people-centricity  |pluralist people-centric; anti-pluralist people-centric; pluralist non-people-centric
moralism3 | candidate attribute - moralism \& anti-elitism  | moralist anti-establishment; non-moralist anti-establishment; non-moralist pro-establishment 
populist2 | candidate attribute - populism (aggregated) | non-populist; populist
immigration2 | candidate attribute - immigration policy | pro-immigration; anti-immigration
economy2 | candidate attribute - economic policy | left; right
populismatt | respondent populist attitudes | non-populist; populist
partyid2 | respondent partisanship | Republican; Democrat

# Main Results

### Figure 1 (original populism coding, congruent policy)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")
plot(mm(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id), vline = 0.5) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")

```

### Figure 2 by populist attitudes (original populism coding, congruent policy)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, by = ~populismatt), 
     group = "populismatt", legend_title = "Populist attitudes") +
  scale_colour_grey(na.translate = F)
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, estimate = "mm", by = ~populismatt), 
     group = "populismatt", vline = 0.5, legend_title = "Populist attitudes") +
  scale_colour_grey(na.translate = F)
```

### Figure 3 by party robustness (original populism coding, congruent policy)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, by = ~partyid2), 
     group = "partyid2", legend_title = "Partisanship") + 
  scale_colour_grey(na.translate = F)
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, estimate = "mm", by = ~partyid2), 
     group = "partyid2", vline = 0.5, legend_title = "Partisanship") +
  scale_colour_grey(na.translate = F)
```

### Table 2 with interactions

```{r warning = F, message = F}
dm5 <- lm(Y ~ job2 + office2 + polls2 + immigration2 + economy2 + populist2 + 
           immigration2*populist2 +  economy2*populist2, data = d_stack)
dm6 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + populist2 + 
            immigration2congruent*populist2 +  economy2congruent*populist2, data = d_stack)

cl.se.dm5 <- sqrt(diag(cluster.vcov(dm5, d_stack$id)))
cl.se.dm6 <- sqrt(diag(cluster.vcov(dm6, d_stack$id)))
```

```{r warning = F, message = F}
stargazer(dm5, dm6,
          dep.var.caption = c(""),
          se=list(cl.se.dm5, cl.se.dm6),
          title="Effects of policy and Populist Rhetoric on vote choice", 
          dep.var.labels=c("Probability of selection"),
          covariate.labels=c("Job background (non-elite)", "Political experience (none)", "Current polling (trailing)", 
                             "Immigration policy (anti)", "Economic policy (right)",
                             "Immigration (congruent)", "Economic policy (congruent)",
                             "Populist rhetoric", 
                             "Immigration policy (anti) : Populist rhetoric",
                             "Economic policy (right) : Populist rhetoric",
                             "Immigration (congruent) : Populist rhetoric",
                             "Economy (congruent) : Populist rhetoric"),
          no.space=TRUE, column.sep.width = "-10pt", 
          omit = c("Constant"),
          omit.stat=c("rsq", "ser", "f"), notes = "", notes.append = F,  
          notes.label = "",
          type = "text",
          star.char = c("+", "*", "**", "***"), star.cutoffs = c(0.05, 0.01, 0.001))
```
# Appendix 

### Figure A1 (alternative coding)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, id = ~id)) + 
  scale_color_manual(values = rep("black", 18)) + ggplot2::theme(legend.position = "none")

plot(mm(d_stack, Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, 
        id = ~id), vline = 0.5) + 
  scale_color_manual(values = rep("black", 18)) + ggplot2::theme(legend.position = "none")
```

### Figure A2 by populist attitudes (alternative coding)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, id = ~id, by = ~populismatt), 
     group = "populismatt", legend_title = "Populist attitudes") + 
  scale_colour_grey(na.translate = F)
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, id = ~id, estimate = "mm", by = ~populismatt), 
     group = "populismatt", vline = 0.5, legend_title = "Populist attitudes") + scale_colour_grey(na.translate = F)

```
### Figure A3 Main results by party (binary populism transformation, original policy)

```{r warning = F, message = F}
plot(cj(d_stack[!is.na(d_stack$partyid2),], Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, id = ~id, by = ~partyid2), 
     group = "partyid2", legend_title = "Partisanship") + 
  scale_colour_grey(na.translate = F)
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2 + economy2 + peoplecentric2 + antipluralist2 + moralist2 + antiestablishment2, id = ~id, estimate = "mm", by = ~partyid2), 
     group = "partyid2", vline = 0.5, legend_title = "Partisanship") + scale_colour_grey(na.translate = F)
```
### Figure A4 for each candidate pair (original populism coding, congruent policy)

```{r warning = F, message = F}
plot(cj(d_stack[d_stack$choiceNum == 1,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")


plot(cj(d_stack[d_stack$choiceNum == 2,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")


plot(cj(d_stack[d_stack$choiceNum == 3,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")


plot(cj(d_stack[d_stack$choiceNum == 4,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")

```

### Figure A5 (attentive respondents only)

```{r warning = F, message = F}
plot(cj(d_stack[d_stack$attention.pass == 1,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id)) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")
plot(mm(d_stack[d_stack$attention.pass == 1,], Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id), vline = 0.5) + scale_color_manual(values = rep("black", 14)) + ggplot2::theme(legend.position = "none")
```
### Figure A6 by strong populist attitudes (original populism coding, congruent policy)

```{r warning = F, message = F}
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, by = ~populismattstr), 
     group = "populismattstr", legend_title = "Populist attitudes") + 
  scale_colour_grey(na.translate = F)
plot(cj(d_stack, Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, id = ~id, estimate = "mm", by = ~populismattstr), 
     group = "populismattstr", vline = 0.5, legend_title = "Populist attitudes") + scale_colour_grey(na.translate = F)
```

### Table A1

```{r}
m1 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3, data = d_stack)
m2 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3 + antipluralism3*moralism3, data = d_stack)
m3 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3 + antipluralism3*moralism3 + partyid2 + partyid2*antipluralism3 + partyid2*moralism3, data = d_stack)
m4 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3 + antipluralism3*moralism3 + populismatt + populismatt*antipluralism3 + populismatt*moralism3, data = d_stack)

cl.se.m1 <- sqrt(diag(cluster.vcov(m1, d_stack$id)))
cl.se.m2 <- sqrt(diag(cluster.vcov(m2, d_stack$id)))
cl.se.m3 <- sqrt(diag(cluster.vcov(m3, d_stack$id)))
cl.se.m4 <- sqrt(diag(cluster.vcov(m4, d_stack$id)))
```

```{r warning = F, message = F}
stargazer(m1, m2, m3, m4, 
          dep.var.caption = "",
          se=list(cl.se.m1, cl.se.m2, cl.se.m3, cl.se.m4), 
          title="Effects of various candidate and speech characteristics on vote choice", 
          dep.var.labels=c("Probability of selection"),
          covariate.labels=c("Job background (non-elite)", "Political experience (none)", "Current polling (trailing)", 
                             "Immigration policy (congruent)", "Economic policy (congruent)", 
                             "Pluralist / People-centric", "Anti-pluralist / People-centric", 
                             "Non-moralist / Anti-establishment", "Moralist / Anti-establishment",
                             "Partisanship (Democrat)", "Populist Attitudes (Populist)", 
                             "Pluralist / People-centric : Non-moralist / Anti-establishment",
                             "Anti-pluralist / People-centric : Non-moralist / Anti-establishment",
                             "Pluralist / People-centric : Moralist / Anti-establishment",
                             "Anti-pluralist / People-centric : Moralist / Anti-establishment",
                             "Pluralist / People-centric : Democrat",
                             "Anti-pluralist / People-centric : Democrat",
                             "Non-moralist / Anti-establishment : Democrat",
                             "Moralist / Anti-establishment : Democrat",
                             "Pluralist / People-centric : Populist",
                             "Anti-pluralist / People-centric : Populist",
                             "Non-moralist / Anti-establishment : Populist",
                             "Moralist / Anti-establishment : Populist"),
          no.space=TRUE, column.sep.width = "-10pt", 
          omit = c("Constant"),
          omit.stat=c("rsq", "ser", "f"), notes = "", notes.append = F,  notes.label = "",
          type="text",
          star.char = c("+", "*", "**", "***"), star.cutoffs = c(0.05, 0.01, 0.001))
```

### Table A2

```{r}
m5 <- lm(Y ~ job2 + office2 + polls2 + immigration2 + economy2 + antipluralism3 + moralism3 + antipluralism3*moralism3 + 
           immigration2*antipluralism3 + immigration2*moralism3 + economy2*antipluralism3 + economy2*moralism3, data = d_stack)
m6 <- lm(Y ~ job2 + office2 + polls2 + immigration2congruent + economy2congruent + antipluralism3 + moralism3 + antipluralism3*moralism3 + 
           immigration2congruent*antipluralism3 + immigration2congruent*moralism3 + economy2congruent*antipluralism3 + economy2congruent*moralism3, data = d_stack)

cl.se.m5 <- sqrt(diag(cluster.vcov(m5, d_stack$id)))
cl.se.m6 <- sqrt(diag(cluster.vcov(m6, d_stack$id)))
```

```{r}
stargazer(m5, m6, 
          dep.var.caption = c(""),
          se=list(cl.se.m5, cl.se.m6), 
          title="Effects of various candidate and speech characteristics on vote choice", 
          dep.var.labels=c("Probability of selection"),
          covariate.labels=c("Job background (non-elite)", "Political experience (none)", "Current polling (trailing)", 
                             "Immigration policy (anti)", "Economic policy (right)",
                             "Immigration (congruent)", "Economic policy (congruent)",
                             "Pluralist / People-centric", "Anti-pluralist / People-centric", 
                             "Non-moralist / Anti-establishment", "Moralist / Anti-establishment",
                             "Pluralist / People-centric : Non-moralist / Anti-establishment",
                             "Anti-pluralist / People-centric : Non-moralist / Anti-establishment",
                             "Pluralist / People-centric : Moralist / Anti-establishment",
                             "Anti-pluralist / People-centric : Moralist / Anti-establishment",
                             "Immigration policy (anti) : Pluralist / People-centric",
                             "Immigration policy (anti) : Anti-pluralist / People-centric",
                             "Immigration policy (anti) : Non-moralist / Anti-establishment",
                             "Immigration policy (anti) : Moralist / Anti-establishment",   
                             "Economic policy (pro) : Pluralist / People-centric",
                             "Economic policy (pro) : Anti-pluralist / People-centric",
                             "Economic policy (pro) : Non-moralist / Anti-establishment",
                             "Economic policy (pro) : Moralist / Anti-establishment",
                             "Immigration (congruent) : Pluralist / People-centric",
                             "Immigration (congruent) : Anti-pluralist / People-centric",
                             "Immigration (congruent) : Non-moralist / Anti-establishment",
                             "Immigration (congruent) : Moralist / Anti-establishment",   
                             "Economy (congruent) : Pluralist / People-centric",
                             "Economy (congruent) : Anti-pluralist / People-centric",
                             "Economy (congruent) : Non-moralist / Anti-establishment",
                             "Economy (congruent) : Moralist / Anti-establishment"),
          no.space=TRUE, column.sep.width = "-10pt", 
          omit = c("Constant"),
          omit.stat=c("rsq", "ser", "f"), notes = "", notes.append = F,  notes.label = "",
          type = "text",
          star.char = c("+", "*", "**", "***"), star.cutoffs = c(0.05, 0.01, 0.001))
```

### Table A3 Descriptive Statistics

```{r}
survey <- read.csv("Survey_clean.csv")
```

```{r}
demographics <- survey[c("age", "Female2", "White2", "College2", "Republican2", "Democrat2", "Populist2")]
```

```{r}
stargazer(demographics,
          covariate.labels=c("Age", "Female", "White", "College+", "Republican",
                             "Democrat", "Populist"), 
          nobs = FALSE, digits=2, type = "text",
          title="Descriptive Statistics (n = 1004/8032)", iqr = TRUE)
```
### codes to turn survey data into conjoint data 

The following codes clean the original survey data and produce the Conjoint_clean.Rdata for the analyses. 

```{r}
# Reshape the candidate attributes so each candidate gets its own row
d_cand = survey %>% select (id, starts_with("choice"))
d_cand = d_cand %>%
  gather (variable , value , -id) %>%
  mutate(
    choiceNum = gsub("[A-Za-z]|_.+", "", variable),
    candNum = gsub(".+(.$)", "\\1", variable),
    attribute = gsub(".+_|.$", "", variable)
  ) %>%
  select(-variable) %>%
  spread(attribute, value)
```

```{r}
# Reshape the respondent's preferences so each choice gets its own row
d_pref = survey %>% select (id, starts_with("pref"))
d_pref = d_pref %>%
  gather(variable, preference, -id) %>%
  mutate(
    choiceNum = gsub("pref", "", variable),
    preference = as.numeric(gsub("Candidate ", "", preference))
  ) %>%
  select (-variable)
```

```{r}
# Merge the attributes and preferences
d_stack = left_join(d_cand,d_pref)
d_stack = d_stack %>%
  mutate(
    Y = as.numeric(candNum == preference)
  )
# Check that you did not create any extra rows
d_stack$candNum <- as.numeric(d_stack$candNum)
d_stack$choiceNum <- as.numeric(d_stack$choiceNum)
nrow(d_stack) == (nrow(data) * max(d_stack$candNum) * max(d_stack$choiceNum))
```
```{r}
# Define/simplify conjoint variables (group attributes)
d_stack$office2 <- factor(ifelse(d_stack$office == "held an office for many years", "held office", "didn't hold office"), levels = c("held office", "didn't hold office"))
d_stack$polls2 <- factor(ifelse(d_stack$polls == "leading", "leading", "trailing"), levels = c("leading", "trailing"))

d_stack$economy2 <- ifelse(d_stack$economy == "I'll increase the current minimum wage to a living American wage." |
                           d_stack$economy == "I believe the richest 1% percent should pay their fair share of taxes." |
                           d_stack$economy == "When I'm in office, we will limit the unfair tax advantages for the rich." |
                           d_stack$economy == "When I'm in office, we'll increase our efforts to help the poor in America.", "left", "right")
d_stack$economy2 <- factor(d_stack$economy2, levels = c("left", "right"))

d_stack$immigration2 <- ifelse(d_stack$immigration == "I believe there should be a path to citizenship." |
                             d_stack$immigration == "I believe American immigration laws are too cruel." |
                             d_stack$immigration == "I'm committed to making our immigration system more open." |
                             d_stack$immigration == "I'll make sure American immigration laws are humane.", "pro-immigration", "anti-immigration")
d_stack$immigration2 <- factor(d_stack$immigration2, levels = c("pro-immigration", "anti-immigration"))
```

```{r}
d_stack$job4 <- mapvalues(d_stack$job, 
                          from = c("businessmen", "farmer", "firefighter", "government advisor", "journalist", "lawyer", "professor", "social worker", "state official", "teacher"), 
                          to = c("economic elite", "non-elite", "non-elite", "political elite", "cultural elite", "economic elite", "cultural elite", "non-elite", "political elite", "non-elite"))
d_stack$job4 <- factor(d_stack$job4, levels = c("non-elite", "economic elite", "cultural elite", "political elite"))
d_stack$job2 <- mapvalues(d_stack$job, 
                          from = c("businessmen", "farmer", "firefighter", "government advisor", "journalist", "lawyer", "professor", "social worker", "state official", "teacher"), 
                          to = c("elite", "non-elite", "non-elite", "elite", "elite", "elite", "elite", "non-elite", "elite", "non-elite"))
d_stack$job2 <- factor(d_stack$job2, levels = c("elite", "non-elite"))

d_stack$economy2 <- ifelse(d_stack$economy == "I'll increase the current minimum wage to a living American wage." |
                             d_stack$economy == "I believe the richest 1% percent should pay their fair share of taxes." |
                             d_stack$economy == "When I'm in office, we will limit the unfair tax advantages for the rich." |
                             d_stack$economy == "When I'm in office, we'll increase our efforts to help the poor in America.", "left", "right")
d_stack$economy2 <- factor(d_stack$economy2, levels = c("left", "right"))
```

```{r}
d_stack$antipluralism3 <- mapvalues(d_stack$pluralism, 
                          from = c("I'll make sure to listen to all the different voices of the people.", 
                                   "I'm running to defend all our rights, no matter our differences.",
                                   "I'll serve everyone in America regardless of their convictions.",
                                   "I am running to represent our diverse American voices.",
                                   "I'll do what is best for America even if the people disagree.",
                                   "I'm running to bring expertise back to politics in Washington.",
                                   "I'll bring the best people to solve our problems in America.",
                                   "As your representative, I will make sure to listen to the experts.",
                                   "I believe the government is to respond to the will of the people.",
                                   "This campaign is not about me, it is about the American people.",
                                   "I believe we, the people, share the same values and interests.",
                                   "I am running to represent the voice of the American people."), 
                          to = c("pluralist / people-centric", "pluralist / people-centric", "pluralist / people-centric", "pluralist / people-centric", 
                                 "pluralist / non-people-centric", "pluralist / non-people-centric", "pluralist / non-people-centric", "pluralist / non-people-centric", 
                                 "anti-pluralist / people-centric", "anti-pluralist / people-centric", "anti-pluralist / people-centric", "anti-pluralist / people-centric"))
d_stack$antipluralism3 <- factor(d_stack$antipluralism3, levels = c("pluralist / non-people-centric", "pluralist / people-centric", "anti-pluralist / people-centric"))
d_stack$antipluralist2 <- mapvalues(d_stack$pluralism, 
                                from = c("I'll make sure to listen to all the different voices of the people.", 
                                         "I'm running to defend all our rights, no matter our differences.",
                                         "I'll serve everyone in America regardless of their convictions.",
                                         "I am running to represent our diverse American voices.",
                                         "I'll do what is best for America even if the people disagree.",
                                         "I'm running to bring expertise back to politics in Washington.",
                                         "I'll bring the best people to solve our problems in America.",
                                         "As your representative, I will make sure to listen to the experts.",
                                         "I believe the government is to respond to the will of the people.",
                                         "This campaign is not about me, it is about the American people.",
                                         "I believe we, the people, share the same values and interests.",
                                         "I am running to represent the voice of the American people."), 
                                to = c("pluralist", "pluralist", "pluralist", "pluralist", 
                                       "pluralist", "pluralist", "pluralist", "pluralist", 
                                       "anti-pluralist", "anti-pluralist", "anti-pluralist", "anti-pluralist"))
d_stack$antipluralist2 <- factor(d_stack$antipluralist2, levels = c("pluralist", "anti-pluralist"))
d_stack$peoplecentric2 <- mapvalues(d_stack$pluralism, 
                                    from = c("I'll make sure to listen to all the different voices of the people.", 
                                             "I'm running to defend all our rights, no matter our differences.",
                                             "I'll serve everyone in America regardless of their convictions.",
                                             "I am running to represent our diverse American voices.",
                                             "I'll do what is best for America even if the people disagree.",
                                             "I'm running to bring expertise back to politics in Washington.",
                                             "I'll bring the best people to solve our problems in America.",
                                             "As your representative, I will make sure to listen to the experts.",
                                             "I believe the government is to respond to the will of the people.",
                                             "This campaign is not about me, it is about the American people.",
                                             "I believe we, the people, share the same values and interests.",
                                             "I am running to represent the voice of the American people."), 
                                    to = c("people-centric", "people-centric", "people-centric", "people-centric", 
                                           "Non-people-centric", "Non-people-centric", "Non-people-centric", "Non-people-centric", 
                                           "people-centric", "people-centric", "people-centric", "people-centric"))
d_stack$peoplecentric2 <- factor(d_stack$peoplecentric2, levels = c("Non-people-centric", "people-centric"))

#levels(factor(d_stack$moralism))
d_stack$moralism3 <- mapvalues(d_stack$moralism, 
                                from = c("But there're bad people in Washington who don't care about Americans.", 
                                         "Sadly, the corrupt Washington elites only listen to special interests.",
                                         "Sadly, Congress is full of insiders who only care about themselves now.",
                                         "I'll protect Americans against all evil in Washington.",
                                         "I'll fix the mess in Washington for the sake of America.",
                                         "Unfortunately, the government has created more problems than it has solved.",
                                         "I believe politicians in Congress talk too much and take too little action.",
                                         "Sadly, so many in Washington are out of touch with the American people.",
                                         "I believe that Washington needs more qualified people like me at the moment.",
                                         "I believe our government is there to help people, even though it's not easy.",
                                         "I trust our Congress is mostly full of honest people who care for Americans.",
                                         "I'll work with all my colleagues in Congress who want what's best for Americans."), 
                                to = c("moralist / anti-establishment", "moralist / anti-establishment", "moralist / anti-establishment", "moralist / anti-establishment", 
                                       "non-moralist / anti-establishment", "non-moralist / anti-establishment", "non-moralist / anti-establishment", "non-moralist / anti-establishment", 
                                       "non-moralist / pro-establishment", "non-moralist / pro-establishment", "non-moralist / pro-establishment", "non-moralist / pro-establishment"))
d_stack$moralism3 <- factor(d_stack$moralism3, levels = c("non-moralist / pro-establishment", "non-moralist / anti-establishment", "moralist / anti-establishment"))
d_stack$moralist2 <- mapvalues(d_stack$moralism, 
                               from = c("But there're bad people in Washington who don't care about Americans.", 
                                        "Sadly, the corrupt Washington elites only listen to special interests.",
                                        "Sadly, Congress is full of insiders who only care about themselves now.",
                                        "I'll protect Americans against all evil in Washington.",
                                        "I'll fix the mess in Washington for the sake of America.",
                                        "Unfortunately, the government has created more problems than it has solved.",
                                        "I believe politicians in Congress talk too much and take too little action.",
                                        "Sadly, so many in Washington are out of touch with the American people.",
                                        "I believe that Washington needs more qualified people like me at the moment.",
                                        "I believe our government is there to help people, even though it's not easy.",
                                        "I trust our Congress is mostly full of honest people who care for Americans.",
                                        "I'll work with all my colleagues in Congress who want what's best for Americans."), 
                               to = c("moralist", "moralist", "moralist", "moralist", 
                                      "non-moralist", "non-moralist", "non-moralist", "non-moralist", 
                                      "non-moralist", "non-moralist", "non-moralist", "non-moralist"))
d_stack$moralist2 <- factor(d_stack$moralist2, levels = c("non-moralist", "moralist"))
d_stack$antiestablishment2 <- mapvalues(d_stack$moralism, 
                               from = c("But there're bad people in Washington who don't care about Americans.", 
                                        "Sadly, the corrupt Washington elites only listen to special interests.",
                                        "Sadly, Congress is full of insiders who only care about themselves now.",
                                        "I'll protect Americans against all evil in Washington.",
                                        "I'll fix the mess in Washington for the sake of America.",
                                        "Unfortunately, the government has created more problems than it has solved.",
                                        "I believe politicians in Congress talk too much and take too little action.",
                                        "Sadly, so many in Washington are out of touch with the American people.",
                                        "I believe that Washington needs more qualified people like me at the moment.",
                                        "I believe our government is there to help people, even though it's not easy.",
                                        "I trust our Congress is mostly full of honest people who care for Americans.",
                                        "I'll work with all my colleagues in Congress who want what's best for Americans."), 
                               to = c("anti-establishment", "anti-establishment", "anti-establishment", "anti-establishment", 
                                      "anti-establishment", "anti-establishment", "anti-establishment", "anti-establishment", 
                                      "pro-establishment", "pro-establishment", "pro-establishment", "pro-establishment"))
d_stack$antiestablishment2 <- factor(d_stack$antiestablishment2, levels = c("pro-establishment", "anti-establishment"))

d_stack$populist2 <- factor(ifelse(d_stack$antipluralist2 == "anti-pluralist" & d_stack$moralist2 == "moralist", "populist", "non-populist"), 
                            levels = c("non-populist", "populist"))
```

```{r}
# Merge with survey data

d_stack <- merge(d_stack, survey[c("id", "partyid3", "partyid2", "populismatt", "populismattstr", "attention.pass")], by = "id")

```

```{r}
# Define party-congruent policy positions
d_stack$economy2congruent <- "non-congruent"
d_stack$economy2congruent <- ifelse(d_stack$economy2 == "left" & d_stack$partyid3 == "Democrat", "congruent", d_stack$economy2congruent)
d_stack$economy2congruent <- ifelse(d_stack$economy2 == "right" & d_stack$partyid3 == "Republican", "congruent", d_stack$economy2congruent)
d_stack$economy2congruent <- ifelse(d_stack$partyid3 == "Independent", NA, d_stack$economy2congruent)
d_stack$economy2congruent <- factor(d_stack$economy2congruent, levels = c("non-congruent", "congruent"))

d_stack$immigration2congruent <- "non-congruent "
d_stack$immigration2congruent <- ifelse(d_stack$immigration2 == "pro-immigration" & d_stack$partyid3 == "Democrat", "congruent ", d_stack$immigration2congruent)
d_stack$immigration2congruent <- ifelse(d_stack$immigration2 == "anti-immigration" & d_stack$partyid3 == "Republican", "congruent ", d_stack$immigration2congruent)
d_stack$immigration2congruent <- ifelse(d_stack$partyid3 == "Independent", NA, d_stack$immigration2congruent)
d_stack$immigration2congruent <- factor(d_stack$immigration2congruent, levels = c("non-congruent ", "congruent "))

d_stack$policy2congruent <- ifelse(d_stack$economy2congruent == " congruent" & d_stack$immigration2congruent == " congruent", " congruent", " non-congruent")
d_stack$policy2congruent <- ifelse(d_stack$partyid3 == "Independent", NA, d_stack$policy2congruent)
d_stack$policy2congruent <- factor(d_stack$policy2congruent, levels = c("non-congruent", "congruent"))
```

```{r}
## Set attribute names
attr(d_stack$job2, "label") <- "Job background"
attr(d_stack$office2, "label") <- "Political experience"
attr(d_stack$polls2, "label") <- "Current polling"
attr(d_stack$populist2, "label") <- "Populism"
attr(d_stack$antipluralist2, "label") <- "Pluralism"
attr(d_stack$moralist2, "label") <- "Moralism"
attr(d_stack$peoplecentric2, "label") <- "People-centrism"
attr(d_stack$antiestablishment2, "label") <- "Anti-establishiment"
attr(d_stack$antipluralism3, "label") <- "Pluralism / People-centricity"
attr(d_stack$moralism3, "label") <- "Moralism / Anti-establishment"
attr(d_stack$economy2, "label") <- "Economic policy"
attr(d_stack$immigration2, "label") <- "Immigration policy"
attr(d_stack$economy2congruent, "label") <- "Economic position"
attr(d_stack$immigration2congruent, "label") <- "Immigration position"
attr(d_stack$policy2congruent, "label") <- "Policy positions"
attr(d_stack$partyid2, "label") <- "Partisanship"
attr(d_stack$populismatt, "label") <- "Populist attitudes"

#save(d_stack, file = "Conjoint_clean.RData")
```


